﻿using ChessWebService.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace ChessWebService.Models
{
    public class TheNapCoinDAO :DataProvider
    {
        public TheNapCoin LayTheNapCoin(string maThe)
        {
            try
            {
                connect();
                command = this.connection.CreateCommand();
                string query = "select * " +
                                "from TheNapCoin where MaThe='" + maThe + "' and TrangThai=0";

                adapter = new SqlDataAdapter(query, connection);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                DataTable dataTable = dataset.Tables[0];
                int n = dataTable.Rows.Count;
                bool abc = false;
                if (n > 0)
                {
                    object ob = GetDataFromDataRow(dataTable, 0);
                    return (TheNapCoin)ob;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            TheNapCoin item = new TheNapCoin();

            item.Id = Convert.ToInt32(dt.Rows[i]["Id"].ToString());
            item.MaThe = dt.Rows[i]["MaThe"].ToString();
            item.MenhGia = float.Parse(dt.Rows[i]["MenhGia"].ToString());
            item.TrangThai = bool.Parse(dt.Rows[i]["TrangThai"].ToString());
            return item;
        }
    }
}